﻿using AdminApi.Extension.Permission;
using AdminApi.Extension.Permission.Attributes;
using AdminApi.Extension.Permission.Model;
using Application.Admin;
using Domain.Types;
using DTO.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using Infrastructure.Error;

namespace AdminApi.Controllers
{
    [ApiController]
    public class TaskCategoryController : ControllerBase
    {
        public TaskAdminApplication taskAdminApplication { get; set; }
        public ILogger<TaskCategoryController> _logger { get; set; }

        [Route("api/TaskCategory/GetTaskCategoryList")]
        [HttpGet, PerssionMenu("任务分类列表", "系统任务管理", Index = "/taskcategory_list")]
        public ActionResult<IList<TaskCategoryOutput>> GetTaskCategoryList()
        {
            try
            {
                var reps = taskAdminApplication.GetTaskCategoryList();
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

        [Route("api/TaskCategory/EditTaskCategory")]
        [HttpPost, PerssionMenu("分类添加/编辑", "系统任务管理-任务分类列表", MenuType = MenuType.添加)]
        public ActionResult<bool> EditTaskCategory(TaskCategoryInput taskCategoryInput)
        {
            try
            {
                foreach (var item in taskCategoryInput.TaskFieldInfos)
                {
                    if (Guid.Empty==item.ID) item.ID = Guid.NewGuid();
                }

                var reps = taskAdminApplication.EditTaskCategory(taskCategoryInput);
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

        [Route("api/TaskCategory/DelTaskCategory")]
        [HttpGet, PerssionMenu("删除分类", "系统任务管理-任务分类列表", MenuType = MenuType.添加)]
        public ActionResult<bool> DelTaskCategory(int cid)
        {
            try
            {
                var reps = taskAdminApplication.DelTaskCategory(cid);
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }

        [Route("api/TaskCategory/EnableTaskCategory")]
        [HttpPost, PerssionMenu("分类启用/禁用", "系统任务管理-任务分类列表", MenuType = MenuType.添加)]
        public ActionResult<bool> EnableTaskCategory(EnableTaskCategoryInput enableTaskCategoryInput)
        {
            try
            {
                var reps = taskAdminApplication.EnableTaskCategory(enableTaskCategoryInput.Cid, enableTaskCategoryInput.Enable);
                return this.Success(reps, "成功");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙!");
            }
        }



    }
}
